<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <title>给多个点添加信息窗体</title>
    <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi" />
    <meta http-equiv="Cache-Control" content="no-siteapp" />
    <link rel="stylesheet" href="css/font.css">
    <link rel="stylesheet" href="css/xadmin.css">
    <script type="text/javascript" src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script>
    <script type="text/javascript"src="https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.min.js"></script>
    <script src="lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="js/xadmin.js"></script>
    <script type="text/javascript" src="js/cookie.js"></script>
    <style>
        html,body,#container{
            height:100%;
            width:100%;
        }
        .info{
            width:26rem;
            margin-left:6.8rem;
            display: none;
        }
    </style>
    <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
    <script type="text/javascript"
            src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id='container'></div>
<div class="info" >
    <h4 id='status'></h4><hr>
    <p id='result'></p><hr>
    <p >由于众多浏览器已不再支持非安全域的定位请求，为保位成功率和精度，请升级您的站点到HTTPS。</p>
</div>
<div class="info" >
    <h4 id="text"></h4>
    <p>当前级别：<span id="map-zoom">15</span></p>
</div>
<div class="input-card" style="width:16rem;display: none">
    <h4>地图缩放相关事件</h4>
    <div>
        <div class="input-item">
            <button id="zoomOn" class="btn" style="margin-right:1rem;">绑定事件</button>
            <button id="zoomOff" class="btn">解绑事件</button>
        </div>
    </div>
</div>
<div class="info">
    <h4>地图限制区域信息</h4>
    <p>NorthEast坐标：<span id="ne" class="lnglat"></span></p>
    <p>SouthWest坐标：<span id="sw" class="lnglat"></span></p>
</div>
<div class="input-card" style="width:14rem;display: none">
    <h4>限制地图显示范围</h4>
    <div class="input-item">
        <button class="btn" id="lock-bounds">限定地图显示范围</button>
    </div>
    <div class="input-item">
        <button class="btn" id="unlock-bounds">取消地图显示限制</button>
    </div>
</div>
<div id="tip"></div>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=d51e66b4b7d9a483b70842cd2c3ccc43"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=716c7e1b1f1687512a85ca471f4f2164&plugin=AMap.Geocoder"></script>
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript">
    var map = new AMap.Map('container', {
        resizeEnable: true
    });
    AMap.plugin('AMap.Geolocation', function() {
        var geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位，默认:true
            timeout: 10000,          //超过10秒后停止定位，默认：5s
            buttonPosition:'RB',    //定位按钮的停靠位置
            buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量，默认：Pixel(10, 20)
            zoomToAccuracy: true,   //定位成功后是否自动调整地图视野到定位点
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition(function(status,result){
            if(status=='complete'){
                onComplete(result)
            }else{
                onError(result)
            }
        });
    });
    //解析定位结果
    function onComplete(data) {
        document.getElementById('status').innerHTML='定位成功'
        var str = [];
        str.push('定位结果：' + data.position);
        sessionStorage.setItem("address",data.position);
        $("#lnglat").val(data.position);
        str.push('定位类别：' + data.location_type);
        if(data.accuracy){
            str.push('精度：' + data.accuracy + ' 米');
        }//如为IP精确定位结果则没有精度信息
        str.push('是否经过偏移：' + (data.isConverted ? '是' : '否'));
        document.getElementById('result').innerHTML = str.join('<br>');
    }
    //解析定位错误信息
    function onError(data) {
        document.getElementById('status').innerHTML='定位失败'
        document.getElementById('result').innerHTML = '失败原因排查信息:'+data.message;
    }

    var logMapinfo = function (){
        var zoom = map.getZoom(); //获取当前地图级别
        document.querySelector("#map-zoom").innerText = zoom;
    };
    setInterval(logMapinfo,100);
    function canResize(){
        if(map.getZoom() < 16){
            map.setZoom(16);
        }
    }
    setInterval(canResize,100);

    var position = sessionStorage.getItem("address").split(",");
    console.log(position);
    var long = position[0];
    var lat = position[1];
    console.log(long);

    var layedit = layui.layedit;
    function showOutlets(){
        $.ajax({
            url:'http://localhost:8081/map/OutletsList',//请求地址
            method:'GET',//请求的方法
            dataType:'JSON',//值类型
            data:{
                Long:long,
                Lat:lat
            },//往后端发送的值
            success:function(res){//成功的回调函数
                console.log("11111");
                console.log(res);
                console.log(res.data.length);
                sessionStorage.setItem("dataLength",res.data.length);
                for(var i = 0;i < res.data.length;i++){
                    sessionStorage.setItem("Long"+i,res.data[i].Long);
                    sessionStorage.setItem("Lat"+i,res.data[i].Lat);
                    sessionStorage.setItem("OutletsName"+i,res.data[i].OutletsName);
                    sessionStorage.setItem("Phone"+i,res.data[i].Phone);
                    sessionStorage.setItem("Position"+i,res.data[i].Position);
                }
            },
            error:function (data) {//失败的回调函数
                alert("系统出错");
            }
        });
    }
    setTimeout(showOutlets,1000);
    console.log("Test")
    var infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
    for (var i = 0, marker; i < sessionStorage.getItem("dataLength"); i++) {
        var marker = new AMap.Marker({
            position: [sessionStorage.getItem("Long"+i),sessionStorage.getItem("Lat"+i)],
            map: map
        });
        marker.content = "<img src='images/cn_store1.png' width='20' height='20'>"+sessionStorage.getItem("OutletsName"+i)+"<br><img src='images/store-point-location.png' width='20' height='20'>"+sessionStorage.getItem("Position"+i)+"<br><img src='images/store-point-tel.png'>"+sessionStorage.getItem("Phone"+i);
        marker.on('click', markerClick);
        marker.emit('click', {target: marker});
    }
    function markerClick(e) {
        infoWindow.setContent(e.target.content);
        infoWindow.open(map, e.target.getPosition());
    }
    map.setFitView();

    //限制范围
    function logMapInfo() {
        var limitBounds = map.getLimitBounds();
        if (limitBounds) {
            document.querySelector("#ne").innerText = limitBounds.northeast.toString();
            document.querySelector("#sw").innerText = limitBounds.southwest.toString();
        } else {
            document.querySelector("#ne").innerText = document.querySelector("#sw").innerText = "未限定";
        }
    }

    //限制地图显示范围
    function lockMapBounds() {
        var bounds = map.getBounds();
        console.log(bounds);
        map.setLimitBounds(bounds);
        logMapInfo();
    }

    //启用地图范围限定
    setTimeout(lockMapBounds,450);
    setTimeout(logMapInfo,450);
</script>
</body>
</html>